\section{Source/Sort.c}


\begin{footnotesize}\begin{alltt}
00001 \textcolor{preprocessor}{#include "Sort.h"}
00002 
00003 \textcolor{keywordtype}{void} Sort\_Bucket(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00004 \{
00005     \textcolor{keywordtype}{int} Index, Found, SmallValue, SmallBucket, Bucket, Buckets=(Length-1)/4+1, In
      dexes[Buckets], Array2[Length];
00006     
00007     \textcolor{comment}{//Copy over data}
00008     \textcolor{keywordflow}{for}(Index=0;Index<Length;++Index)
00009         Array2[Index]=Array[Index+Offset];
00010     
00011     \textcolor{comment}{//Sort each bucket}
00012     Index=0;
00013     \textcolor{keywordflow}{while}(Index<Length)
00014     \{
00015         \textcolor{keywordflow}{switch}(Length-Index)
00016         \{
00017             \textcolor{keywordflow}{case} 1:
00018             \textcolor{keywordflow}{break};
00019             \textcolor{keywordflow}{case} 2:
00020                 Sort\_Rapid2(Array2, Index);
00021             \textcolor{keywordflow}{break};
00022             \textcolor{keywordflow}{case} 3:
00023                 Sort\_Rapid3(Array2, Index);
00024             \textcolor{keywordflow}{break};
00025             \textcolor{keywordflow}{default}:
00026                 Sort\_Rapid4(Array2, Index);
00027             \textcolor{keywordflow}{break};
00028         \}
00029         Index+=4;
00030     \}
00031     
00032     \textcolor{comment}{//Merge back together}
00033     \textcolor{keywordflow}{for}(Bucket=0;Bucket<Buckets;++Bucket)
00034         Indexes[Bucket]=0;
00035         
00036     SmallValue=SmallBucket=0;
00037     \textcolor{keywordflow}{for}(Index=0;Index<Length;++Index)
00038     \{
00039         Found=0;
00040         \textcolor{keywordflow}{for}(Bucket=0;Bucket<Buckets;++Bucket)
00041         \{
00042             \textcolor{keywordflow}{if} (Indexes[Bucket]<4 && (Indexes[Bucket]+Bucket*4)<Length && (Found=
      =0 || Array2[Indexes[Bucket]+Bucket*4]<SmallValue))
00043             \{
00044                 Found=1;
00045                 SmallValue=Array2[Indexes[Bucket]+Bucket*4];
00046                 SmallBucket=Bucket;
00047             \}
00048         \}
00049         
00050         Array[Index+Offset]=SmallValue;
00051         ++Indexes[SmallBucket];
00052     \}
00053 \}
00054 
00055 \textcolor{keywordtype}{void} Sort\_Dan(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00056 \{
00057     \textcolor{keywordtype}{int} Temp, Index, SmallValue, SmallIndex, BigValue, BigIndex;
00058     
00059     \textcolor{keywordflow}{switch}(Length)
00060     \{
00061         \textcolor{keywordflow}{case} -1:
00062         \textcolor{keywordflow}{case} 0:
00063         \textcolor{keywordflow}{case} 1:
00064             \textcolor{keywordflow}{return};
00065         \textcolor{keywordflow}{break};
00066         \textcolor{keywordflow}{case} 2:
00067             \textcolor{keywordflow}{if} (Array[Offset]>Array[Offset+1])
00068             \{
00069                 Temp=Array[Offset];
00070                 Array[Offset]=Array[Offset+1];
00071                 Array[Offset+1]=Temp;
00072             \}
00073             \textcolor{keywordflow}{return};
00074         \textcolor{keywordflow}{break};
00075         \textcolor{keywordflow}{case} 3:
00076             Sort\_Rapid3(Array, Offset);
00077             \textcolor{keywordflow}{return};
00078         \textcolor{keywordflow}{break};
00079         \textcolor{keywordflow}{case} 4:
00080             Sort\_Rapid4(Array, Offset);
00081             \textcolor{keywordflow}{return};
00082         \textcolor{keywordflow}{break};
00083         \textcolor{keywordflow}{default}:
00084             SmallValue=Array[Offset];
00085             BigValue=Array[Offset];
00086             SmallIndex=Offset;
00087             BigIndex=Offset;
00088             \textcolor{keywordflow}{for}(Index=Offset;Index<Offset+Length;++Index)
00089             \{
00090                 Temp=Array[Index];
00091                 
00092                 \textcolor{keywordflow}{if} (Temp<SmallValue)
00093                 \{
00094                     SmallValue=Temp;
00095                     SmallIndex=Index;
00096                 \}
00097                 \textcolor{keywordflow}{else} \textcolor{keywordflow}{if}(Temp>BigValue)
00098                 \{
00099                     BigValue=Temp;
00100                     BigIndex=Index;
00101                 \}
00102             \}
00103             
00104             Temp=Array[Offset];
00105             Array[Offset]=Array[SmallIndex];
00106             Array[SmallIndex]=Temp;
00107             
00108             Temp=Array[Offset+Length-1];
00109             Array[Offset+Length-1]=Array[BigIndex];
00110             Array[BigIndex]=Temp;
00111             
00112             Sort\_Dan(Array, Offset+1, Length-2);
00113         \textcolor{keywordflow}{break};
00114     \}
00115 \}
00116 
00117 \textcolor{keywordtype}{void} Sort\_Quick(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00118 \{
00119     \textcolor{keywordtype}{int} Temp, Pivot, Index, SwapIndex;
00120     
00121     \textcolor{keywordflow}{switch}(Length)
00122     \{
00123         \textcolor{keywordflow}{case} -1:
00124         \textcolor{keywordflow}{case} 0:
00125         \textcolor{keywordflow}{case} 1:
00126             \textcolor{keywordflow}{return};
00127         \textcolor{keywordflow}{break};
00128         \textcolor{keywordflow}{case} 2:
00129             \textcolor{keywordflow}{if} (Array[Offset]>Array[Offset+1])
00130             \{
00131                 Temp=Array[Offset];
00132                 Array[Offset]=Array[Offset+1];
00133                 Array[Offset+1]=Temp;
00134             \}
00135             \textcolor{keywordflow}{return};
00136         \textcolor{keywordflow}{break};
00137         \textcolor{keywordflow}{case} 3:
00138             Sort\_Rapid3(Array, Offset);
00139             \textcolor{keywordflow}{return};
00140         \textcolor{keywordflow}{break};
00141         \textcolor{keywordflow}{case} 4:
00142             Sort\_Rapid4(Array, Offset);
00143             \textcolor{keywordflow}{return};
00144         \textcolor{keywordflow}{break};
00145         \textcolor{comment}{/*case 5:}
00146 \textcolor{comment}{            Sort\_Rapid5(Array, Offset);}
00147 \textcolor{comment}{            return;}
00148 \textcolor{comment}{        break;*/}
00149         \textcolor{keywordflow}{default}:
00150             \textcolor{comment}{//Use last value as pivot}
00151             Pivot=Array[Offset+Length-1];
00152             
00153             \textcolor{keywordflow}{for}(Index=SwapIndex=Offset;Index<Offset+Length;++Index)
00154             \{
00155                 \textcolor{keywordflow}{if} ((Temp=Array[Index])<=Pivot)
00156                 \{
00157                     Array[Index]=Array[SwapIndex];
00158                     Array[SwapIndex++]=Temp;
00159                 \}
00160             \}
00161             
00162             \textcolor{comment}{/* Now sort each part */}
00163             Sort\_Quick(Array, Offset, SwapIndex-Offset-1);
00164             Sort\_Quick(Array, SwapIndex, (Offset+Length)-SwapIndex);
00165             \textcolor{keywordflow}{return};
00166         \textcolor{keywordflow}{break};
00167     \}
00168 \}
00169 
00170 \textcolor{keywordtype}{void} Sort\_Bubble(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00171 \{
00172     \textcolor{keywordtype}{int} Changes=1, Index, Temp;
00173     
00174     \textcolor{keywordflow}{while}(Changes)
00175     \{
00176         Changes=0;
00177         
00178         \textcolor{keywordflow}{for}(Index=Offset;Index+1<Offset+Length;++Index)
00179         \{
00180             Temp=Array[Index];
00181             
00182             \textcolor{keywordflow}{if}(Temp>Array[Index+1])
00183             \{
00184                 Changes=1;
00185                 
00186                 Array[Index]=Array[Index+1];
00187                 Array[Index+1]=Temp;
00188             \}
00189         \}
00190     \}
00191     
00192     \textcolor{keywordflow}{return};
00193 \}
00194 
00195 \textcolor{keywordtype}{void} Sort\_Heap(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00196 \{
00197     \textcolor{keywordtype}{int} CurrentLength, Index, BiggestIndex, BiggestValue;
00198     
00199     \textcolor{keywordflow}{for}(CurrentLength=Length;CurrentLength>1;--CurrentLength)
00200     \{
00201         Index=Offset;
00202         
00203         BiggestValue=Array[Index];
00204         BiggestIndex=Index;
00205         ++Index;
00206         \textcolor{keywordflow}{while}(Index<CurrentLength+Offset)
00207         \{
00208             \textcolor{keywordflow}{if} (Array[Index]>BiggestValue)
00209             \{
00210                 BiggestValue=Array[Index];
00211                 BiggestIndex=Index;
00212             \}
00213             
00214             ++Index;
00215         \}
00216         
00217         Array[BiggestIndex]=Array[CurrentLength+Offset-1];
00218         Array[CurrentLength+Offset-1]=BiggestValue;
00219     \}
00220     
00221     \textcolor{keywordflow}{return};
00222 \}
00223 
00224 
00225 \textcolor{keywordtype}{void} Sort\_Cocktail(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00226 \{
00227     \textcolor{keywordtype}{int} Changes=1, Index, Temp;
00228     
00229     \textcolor{keywordflow}{while}(Changes)
00230     \{
00231         Changes=0;
00232         
00233         \textcolor{keywordflow}{for}(Index=Offset;Index+1<Offset+Length;++Index)
00234         \{
00235             Temp=Array[Index];
00236             
00237             \textcolor{keywordflow}{if}(Temp>Array[Index+1])
00238             \{
00239                 Changes=1;
00240                 
00241                 Array[Index]=Array[Index+1];
00242                 Array[Index+1]=Temp;
00243             \}
00244         \}
00245         \textcolor{keywordflow}{for}(Index=Offset+Length-1;Index>0;--Index)
00246         \{
00247             Temp=Array[Index];
00248             
00249             \textcolor{keywordflow}{if}(Temp<Array[Index-1])
00250             \{
00251                 Changes=1;
00252                 
00253                 Array[Index]=Array[Index-1];
00254                 Array[Index-1]=Temp;
00255             \}
00256         \}
00257     \}
00258     
00259     \textcolor{keywordflow}{return};
00260 \}
00261 
00262 \textcolor{keywordtype}{void} Sort\_Insert(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset, \textcolor{keywordtype}{int} Length)
00263 \{
00264     \textcolor{keywordtype}{int} InputArray[Length];
00265     \textcolor{keywordtype}{int} Index, InputIndex, Current, OutputIndex, ShiftIndex;
00266     
00267     \textcolor{comment}{/* Copy over Array */}
00268     \textcolor{keywordflow}{for}(Index=0;Index<Length;++Index)
00269         InputArray[Index]=Array[Index+Offset];
00270     
00271     \textcolor{keywordflow}{for}(InputIndex=0;InputIndex<Length;++InputIndex)
00272     \{
00273         Current=InputArray[InputIndex];
00274         
00275         \textcolor{keywordflow}{if} (InputIndex!=0)
00276         \{       
00277             Index=0;
00278             OutputIndex=InputIndex;
00279             \textcolor{keywordflow}{while}(Index<InputIndex)
00280             \{
00281                 \textcolor{keywordflow}{if} (Current<Array[Index+Offset])
00282                 \{
00283                     OutputIndex=Index;
00284                     Index=InputIndex;
00285                 \}
00286                 
00287                 ++Index;
00288             \}
00289             OutputIndex+=Offset;
00290             
00291             \textcolor{comment}{/* Shift everything forward */}
00292             \textcolor{keywordflow}{for}(ShiftIndex=InputIndex+1+Offset;ShiftIndex>OutputIndex;--ShiftInde
      x)
00293                 Array[ShiftIndex]=Array[ShiftIndex-1];
00294         \}
00295         \textcolor{keywordflow}{else}
00296             OutputIndex=0;
00297         
00298         \textcolor{comment}{/* Put value in right place */}
00299         Array[OutputIndex]=Current;
00300     \}
00301     
00302     
00303     \textcolor{keywordflow}{return};
00304 \}
00305 
00306 \textcolor{keyword}{inline} \textcolor{keywordtype}{void} Sort\_Rapid2(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset)
00307 \{
00308     \textcolor{keywordtype}{int} Temp;
00309     \textcolor{keywordflow}{if} (Array[Offset]>Array[Offset+1])
00310     \{
00311         Temp=Array[Offset];
00312         Array[Offset]=Array[Offset+1];
00313         Array[Offset+1]=Temp;
00314     \}
00315     
00316     \textcolor{keywordflow}{return};
00317 \}
00318 
00319 \textcolor{keyword}{inline} \textcolor{keywordtype}{void} Sort\_Rapid3(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset)
00320 \{
00321     \textcolor{keywordtype}{int} A=Array[Offset+0];
00322     \textcolor{keywordtype}{int} B=Array[Offset+1];
00323     \textcolor{keywordtype}{int} C=Array[Offset+2];
00324     
00325     \textcolor{keywordflow}{if} (A>B)
00326     \{
00327         \textcolor{comment}{//A>B}
00328         \textcolor{keywordflow}{if} (B>C)
00329         \{
00330             \textcolor{comment}{//A>B>C}
00331             Array[Offset+0]=C;Array[Offset+1]=B;Array[Offset+2]=A;\textcolor{keywordflow}{return};
00332         \}
00333         \textcolor{keywordflow}{else}
00334         \{
00335             \textcolor{comment}{//A>B C>B}
00336             \textcolor{keywordflow}{if} (A>C)
00337             \{
00338                 \textcolor{comment}{//A>C>B}
00339                 Array[Offset+0]=B;Array[Offset+1]=C;Array[Offset+2]=A;\textcolor{keywordflow}{return};
00340             \}
00341             \textcolor{keywordflow}{else}
00342             \{
00343                 \textcolor{comment}{//C>A>B}
00344                 Array[Offset+0]=B;Array[Offset+1]=A;Array[Offset+2]=C;\textcolor{keywordflow}{return};
00345             \}
00346         \}
00347     \}
00348     \textcolor{keywordflow}{else}
00349     \{
00350         \textcolor{comment}{//B>A}
00351         \textcolor{keywordflow}{if} (A>C)
00352         \{
00353             \textcolor{comment}{//B>A>C}
00354             Array[Offset+0]=C;Array[Offset+1]=A;Array[Offset+2]=B;\textcolor{keywordflow}{return};
00355         \}
00356         \textcolor{keywordflow}{else}
00357         \{
00358             \textcolor{comment}{//B>A C>A}
00359             \textcolor{keywordflow}{if} (B>C)
00360             \{
00361                 \textcolor{comment}{//B>C>A}
00362                 Array[Offset+0]=A;Array[Offset+1]=C;Array[Offset+2]=B;\textcolor{keywordflow}{return};
00363             \}
00364             \textcolor{keywordflow}{else}
00365             \{
00366                 \textcolor{comment}{//C>B>A}
00367                 Array[Offset+0]=A;Array[Offset+1]=B;Array[Offset+2]=C;\textcolor{keywordflow}{return};
00368             \}
00369         \}
00370     \}
00371 \}
00372 
00373 \textcolor{keyword}{inline} \textcolor{keywordtype}{void} Sort\_Rapid4(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset)
00374 \{
00375     \textcolor{keywordtype}{int} A=Array[Offset+0];
00376     \textcolor{keywordtype}{int} B=Array[Offset+1];
00377     \textcolor{keywordtype}{int} C=Array[Offset+2];
00378     \textcolor{keywordtype}{int} D=Array[Offset+3];
00379     
00380     \textcolor{keywordflow}{if} (A>B)
00381     \{
00382         \textcolor{comment}{// A>B}
00383         \textcolor{keywordflow}{if} (C>D)
00384         \{
00385             \textcolor{comment}{// A>B C>D}
00386             \textcolor{keywordflow}{if} (C>A)
00387             \{
00388                 \textcolor{comment}{// C>A>B C>D}
00389                 \textcolor{keywordflow}{if} (D<A)
00390                 \{
00391                     \textcolor{comment}{// C>A>B C>D D<A}
00392                     \textcolor{keywordflow}{if} (D<B)
00393                     \{
00394                         \textcolor{comment}{// C>A>B>D}
00395                         Array[Offset+0]=D;Array[Offset+1]=B;Array[Offset+2]=A;Arr
      ay[Offset+3]=C;\textcolor{keywordflow}{return};
00396                     \}
00397                     \textcolor{keywordflow}{else}
00398                     \{
00399                         \textcolor{comment}{// C>A>D>B}
00400                         Array[Offset+0]=B;Array[Offset+1]=D;Array[Offset+2]=A;Arr
      ay[Offset+3]=C;\textcolor{keywordflow}{return};
00401                     \}
00402                 \}
00403                 \textcolor{keywordflow}{else}
00404                 \{
00405                     \textcolor{comment}{// C>D>A>B}
00406                     Array[Offset+0]=B;Array[Offset+1]=A;Array[Offset+2]=D;Array[O
      ffset+3]=C;\textcolor{keywordflow}{return};
00407                 \}
00408             \}
00409             \textcolor{keywordflow}{else}
00410             \{
00411                 \textcolor{comment}{// A>B A>C C>D}
00412                 \textcolor{keywordflow}{if} (B>C)
00413                 \{
00414                     \textcolor{comment}{//A>B>C>D}
00415                     Array[Offset+0]=D;Array[Offset+1]=C;Array[Offset+2]=B;Array[O
      ffset+3]=A;\textcolor{keywordflow}{return};
00416                 \}
00417                 \textcolor{keywordflow}{else}
00418                 \{
00419                     \textcolor{comment}{//A>C>B C>D}
00420                     \textcolor{keywordflow}{if} (D>B)
00421                     \{
00422                         \textcolor{comment}{//A>C>D>B}
00423                         Array[Offset+0]=B;Array[Offset+1]=D;Array[Offset+2]=C;Arr
      ay[Offset+3]=A;\textcolor{keywordflow}{return};
00424                     \}
00425                     \textcolor{keywordflow}{else}
00426                     \{
00427                         \textcolor{comment}{//A>C>B>D}
00428                         Array[Offset+0]=D;Array[Offset+1]=B;Array[Offset+2]=C;Arr
      ay[Offset+3]=A;\textcolor{keywordflow}{return};
00429                     \}
00430                 \}
00431             \}
00432         \}
00433         \textcolor{keywordflow}{else}
00434         \{
00435             \textcolor{comment}{// A>B D>C}
00436             \textcolor{keywordflow}{if} (D>A)
00437             \{
00438                 \textcolor{comment}{//D>A>B D>C}
00439                 \textcolor{keywordflow}{if} (C>B)
00440                 \{
00441                     \textcolor{comment}{//D>A>B D>C>B}
00442                     \textcolor{keywordflow}{if} (C>A)
00443                     \{
00444                         \textcolor{comment}{//D>C>A>B}
00445                         Array[Offset+0]=B;Array[Offset+1]=A;Array[Offset+2]=C;Arr
      ay[Offset+3]=D;\textcolor{keywordflow}{return};
00446                     \}
00447                     \textcolor{keywordflow}{else}
00448                     \{
00449                         \textcolor{comment}{//D>A>C>B}
00450                         Array[Offset+0]=B;Array[Offset+1]=C;Array[Offset+2]=A;Arr
      ay[Offset+3]=D;\textcolor{keywordflow}{return};
00451                     \}
00452                 \}
00453                 \textcolor{keywordflow}{else}
00454                 \{
00455                     \textcolor{comment}{//D>A>B>C}
00456                     Array[Offset+0]=C;Array[Offset+1]=B;Array[Offset+2]=A;Array[O
      ffset+3]=D;\textcolor{keywordflow}{return};
00457                 \}
00458             \}
00459             \textcolor{keywordflow}{else}
00460             \{
00461                 \textcolor{comment}{//A>B A>D>C}
00462                 \textcolor{keywordflow}{if} (B<C)
00463                 \{
00464                     \textcolor{comment}{//A>D>C>B}
00465                     Array[Offset+0]=B;Array[Offset+1]=C;Array[Offset+2]=D;Array[O
      ffset+3]=A;\textcolor{keywordflow}{return};
00466                 \}
00467                 \textcolor{keywordflow}{else}
00468                 \{
00469                     \textcolor{comment}{//A>B>C A>D>C}
00470                     \textcolor{keywordflow}{if} (B>D)
00471                     \{
00472                         \textcolor{comment}{//A>B>D>C}
00473                         Array[Offset+0]=C;Array[Offset+1]=D;Array[Offset+2]=B;Arr
      ay[Offset+3]=A;\textcolor{keywordflow}{return};
00474                     \}
00475                     \textcolor{keywordflow}{else}
00476                     \{
00477                         \textcolor{comment}{//A>D>B>C}
00478                         Array[Offset+0]=C;Array[Offset+1]=B;Array[Offset+2]=D;Arr
      ay[Offset+3]=A;\textcolor{keywordflow}{return};
00479                     \}
00480                 \}
00481             \}
00482         \}
00483     \}
00484     \textcolor{keywordflow}{else}
00485     \{
00486         \textcolor{comment}{// B>A}
00487         \textcolor{keywordflow}{if} (C>D)
00488         \{
00489             \textcolor{comment}{// B>A C>D}
00490             \textcolor{keywordflow}{if} (C>B)
00491             \{
00492                 \textcolor{comment}{// C>B>A C>D}
00493                 \textcolor{keywordflow}{if} (D<B)
00494                 \{
00495                     \textcolor{comment}{// C>B>A C>D D<B}
00496                     \textcolor{keywordflow}{if} (D<A)
00497                     \{
00498                         \textcolor{comment}{// C>B>A>D}
00499                         Array[Offset+0]=D;Array[Offset+1]=A;Array[Offset+2]=B;Arr
      ay[Offset+3]=C;\textcolor{keywordflow}{return};
00500                     \}
00501                     \textcolor{keywordflow}{else}
00502                     \{
00503                         \textcolor{comment}{// C>B>D>A}
00504                         Array[Offset+0]=A;Array[Offset+1]=D;Array[Offset+2]=B;Arr
      ay[Offset+3]=C;\textcolor{keywordflow}{return};
00505                     \}
00506                 \}
00507                 \textcolor{keywordflow}{else}
00508                 \{
00509                     \textcolor{comment}{// C>D>B>A}
00510                     Array[Offset+0]=A;Array[Offset+1]=B;Array[Offset+2]=D;Array[O
      ffset+3]=C;\textcolor{keywordflow}{return};
00511                 \}
00512             \}
00513             \textcolor{keywordflow}{else}
00514             \{
00515                 \textcolor{comment}{// B>A B>C C>D}
00516                 \textcolor{keywordflow}{if} (A>C)
00517                 \{
00518                     \textcolor{comment}{//B>A>C>D}
00519                     Array[Offset+0]=D;Array[Offset+1]=C;Array[Offset+2]=A;Array[O
      ffset+3]=B;\textcolor{keywordflow}{return};
00520                 \}
00521                 \textcolor{keywordflow}{else}
00522                 \{
00523                     \textcolor{comment}{//B>C>A C>D}
00524                     \textcolor{keywordflow}{if} (D>A)
00525                     \{
00526                         \textcolor{comment}{//B>C>D>A}
00527                         Array[Offset+0]=A;Array[Offset+1]=D;Array[Offset+2]=C;Arr
      ay[Offset+3]=B;\textcolor{keywordflow}{return};
00528                     \}
00529                     \textcolor{keywordflow}{else}
00530                     \{
00531                         \textcolor{comment}{//B>C>A>D}
00532                         Array[Offset+0]=D;Array[Offset+1]=A;Array[Offset+2]=C;Arr
      ay[Offset+3]=B;\textcolor{keywordflow}{return};
00533                     \}
00534                 \}
00535             \}
00536         \}
00537         \textcolor{keywordflow}{else}
00538         \{
00539             \textcolor{comment}{// B>A D>C}
00540             \textcolor{keywordflow}{if} (D>B)
00541             \{
00542                 \textcolor{comment}{//D>B>A D>C}
00543                 \textcolor{keywordflow}{if} (C>A)
00544                 \{
00545                     \textcolor{comment}{//D>B>A D>C>A}
00546                     \textcolor{keywordflow}{if} (C>B)
00547                     \{
00548                         \textcolor{comment}{//D>C>B>A}
00549                         Array[Offset+0]=A;Array[Offset+1]=B;Array[Offset+2]=C;Arr
      ay[Offset+3]=D;\textcolor{keywordflow}{return};
00550                     \}
00551                     \textcolor{keywordflow}{else}
00552                     \{
00553                         \textcolor{comment}{//D>B>C>A}
00554                         Array[Offset+0]=A;Array[Offset+1]=C;Array[Offset+2]=B;Arr
      ay[Offset+3]=D;\textcolor{keywordflow}{return};
00555                     \}
00556                 \}
00557                 \textcolor{keywordflow}{else}
00558                 \{
00559                     \textcolor{comment}{//D>B>A>C}
00560                     Array[Offset+0]=C;Array[Offset+1]=A;Array[Offset+2]=B;Array[O
      ffset+3]=D;\textcolor{keywordflow}{return};
00561                 \}
00562             \}
00563             \textcolor{keywordflow}{else}
00564             \{
00565                 \textcolor{comment}{//B>A B>D>C}
00566                 \textcolor{keywordflow}{if} (A<C)
00567                 \{
00568                     \textcolor{comment}{//B>D>C>A}
00569                     Array[Offset+0]=A;Array[Offset+1]=C;Array[Offset+2]=D;Array[O
      ffset+3]=B;\textcolor{keywordflow}{return};
00570                 \}
00571                 \textcolor{keywordflow}{else}
00572                 \{
00573                     \textcolor{comment}{//B>A>C B>D>C}
00574                     \textcolor{keywordflow}{if} (A>D)
00575                     \{
00576                         \textcolor{comment}{//B>A>D>C}
00577                         Array[Offset+0]=C;Array[Offset+1]=D;Array[Offset+2]=A;Arr
      ay[Offset+3]=B;\textcolor{keywordflow}{return};
00578                     \}
00579                     \textcolor{keywordflow}{else}
00580                     \{
00581                         \textcolor{comment}{//B>D>A>C}
00582                         Array[Offset+0]=C;Array[Offset+1]=A;Array[Offset+2]=D;Arr
      ay[Offset+3]=B;\textcolor{keywordflow}{return};
00583                     \}
00584                 \}
00585             \}
00586         \}
00587     \}
00588 \}
00589 
00590 \textcolor{keyword}{inline} \textcolor{keywordtype}{void} Sort\_Rapid5(\textcolor{keywordtype}{int} *Array, \textcolor{keywordtype}{int} Offset)
00591 \{
00592     
00593     Sort\_Rapid4(Array, Offset);
00594     Sort\_Rapid4(Array, Offset+1);
00595     Sort\_Rapid2(Array, Offset);
00596     
00597     
00598     \textcolor{comment}{/* Should be possible in 7 comparisons (one above takes 11) */}
00599 \}
\end{alltt}\end{footnotesize}
